package wisCodeGener;

import java.io.File;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {

	// 首字母转小写
	public static String toLowerCaseFirstOne(String s) {
		if (Character.isLowerCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
	}

	// 首字母转大写
	public static String toUpperCaseFirstOne(String s) {
		if (Character.isUpperCase(s.charAt(0)))
			return s;
		else
			return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
	}

	public static boolean createDirectory(String folder) {
		File dir = new File(folder);
		if (dir.exists()) {
			return true;
		} else {
			return dir.mkdirs();
		}
	}

	/**
	 * 模板解析
	 * 
	 * @param tempalte
	 *            {{tableName}}.xh=1
	 * @param parameters
	 *            { tableName:T_XJGL_XSXX_XSJCXX }
	 * @return T_XJGL_XSXX_XSJCXX.xh=1
	 */
	public static String parseTemplate(String tempalte, Map<String, String> parameters) {
		Pattern p = Pattern.compile("(\\{\\{([a-zA-Z0-9_]+)\\}\\})");
		Matcher m = p.matcher(tempalte);
		StringBuffer stringBuffer = new StringBuffer();
		while (m.find()) {
			String key = m.group(2);
			String value = null;
			if (parameters.containsKey(key)) {
				value = parameters.get(key);
			}
			value = (value == null) ? "" : value;
			m.appendReplacement(stringBuffer, value);
		}
		m.appendTail(stringBuffer);
		return stringBuffer.toString();
	}
}
